package cn.seecoder.ai.dao.converter;

import cn.seecoder.ai.enums.TrainParamEnum;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import javax.persistence.AttributeConverter;
import java.util.Map;

/**
 * @author fanyanpeng
 * @date 2023/4/20 4:54
 */
public class ObjectConverter implements AttributeConverter<Object,String> {
    /**
     * Converts the value stored in the entity attribute into the
     * data representation to be stored in the database.
     *
     * @param attribute the entity attribute value to be converted
     * @return the converted data to be stored in the database
     * column
     */
    @Override
    public String convertToDatabaseColumn(Object attribute) {
        return JSON.toJSONString(attribute);
    }

    /**
     * Converts the data stored in the database column into the
     * value to be stored in the entity attribute.
     * Note that it is the responsibility of the converter writer to
     * specify the correct <code>dbData</code> type for the corresponding
     * column for use by the JDBC driver: i.e., persistence providers are
     * not expected to do such type conversion.
     *
     * @param dbData the data from the database column to be
     *               converted
     * @return the converted value to be stored in the entity
     * attribute
     */
    @Override
    public Object convertToEntityAttribute(String dbData) {
        return JSON.parseObject(dbData,Object.class);
    }
}
